<script setup lang="ts">
import { getCodeAPI, postLoginAPI } from '@/apis/common'
import type { UniFormsRules } from '@uni-helper/uni-ui-types'
import { ref } from 'vue'
import { useUserStore } from '@/stores/user'

defineOptions({
  options: {
    styleIsolation: 'shared',
  },
})
//勾选框
const showCountDown = ref(false)

//获取验证码
const getMobileCode = async () => {
  try {
    await formRef.value?.validateField?.('mobile')
    showCountDown.value = true
    const res = await getCodeAPI({ mobile: loginForm.value.mobile, type: 'login' })
    console.log('测试阶段返回值有验证码', res)
  } catch (err) {
    console.log(err)
  }
}

//表单数据
const loginForm = ref({
  mobile: '13230000019',
  code: '',
})
//表单规则校验
const formRules = ref<UniHelper.UniFormsRules>({
  mobile: {
    rules: [
      { required: true, errorMessage: '请填写手机号码' },
      { pattern: /^1\d{10}$/, errorMessage: '手机号码格式不正确' },
    ],
  },
  code: {
    rules: [
      { required: true, errorMessage: '请输入验证码' },
      { pattern: /^\d{6}$/, errorMessage: '验证码格式不正确' },
    ],
  },
})
//提交表单
const onSubmit = async () => {
  try {
    if (!isAgree.value) {
      uni.showToast({
        title: '请先同意用户协议和隐私协议',
        icon: 'none',
      })
      return
    }
    await formRef.value?.validate?.()
    const res = await postLoginAPI(loginForm.value)
    // 登录成功，保存 token
    const store = useUserStore()
    store.setToken(res.data.token)
    store.setUserInfo(res.data)
    // 提示登录成功
    uni.showToast({
      title: '登录成功',
      icon: 'success',
    })
    uni.switchTab({ url: '/pages/index/index' })
  } catch (err) {
    console.log(err)
  }
}
const formRef = ref<UniHelper.UniFormsInstance>()
const isAgree = ref(false)
</script>

<template>
  <uni-forms class="login-form" ref="formRef" :value="loginForm" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        :maxlength="11"
        v-model="loginForm.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
        :maxlength="6"
        v-model="loginForm.code"
      />
      <text class="text-button" v-if="!showCountDown" @click="getMobileCode">获取验证码</text>
      <view class="text-button" v-else>
        <uni-countdown
          :show-day="false"
          :show-hour="false"
          :show-minute="false"
          :second="5"
          color="#16c2a3"
          @timeup="showCountDown = false"
        />秒后重新发送
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="isAgree = !isAgree" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @tap="onSubmit">登 录</button>
    {{ loginForm }}
  </uni-forms>
</template>

<style lang="scss">
@import './style.scss';
</style>
